package com.neusoft.neuiotms.dcom.converter.change;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeCiPropertyDTO;
import com.neusoft.neuiotms.dcom.dto.change.ChangeCiRelationDTO;
import com.neusoft.neuiotms.dcom.dto.change.ChangeImplementationDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeCiProperty;
import com.neusoft.neuiotms.dcom.model.change.ChangeCiRelation;
import com.neusoft.neuiotms.dcom.model.change.ChangeImplementation;
import com.neusoft.neuiotms.dcom.model.change.ChangeImplementationAttachment;
import com.neusoft.neuiotms.dcom.repository.change.ChangeImplementationRepository;

@Component
public class ChangeImplementationConverter extends AbstractConverter<ChangeImplementation, ChangeImplementationDTO> {

    @Autowired
    private ChangeImplementationRepository changeImplementationRepository;

    @Autowired
    private ChangeImplementationAttachmentConverter changeImplementationAttachmentConverter;

    @Autowired
    private ChangeCiPropertyConverter changeCiPropertyConverter;

    @Autowired
    private ChangeCiRelationConverter changeCiRelationConverter;

    @Override
    public ChangeImplementation toModel(final ChangeImplementationDTO dto) {
        ChangeImplementation model;
        if (dto.isNew()) {
            model = new ChangeImplementation();
        } else {
            model = this.changeImplementationRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setActualStartTime(dto.getActualStartTime());
        model.setActualEndTime(dto.getActualEndTime());
        model.setImplementationContent(dto.getImplementationContent());
        model.setResult(dto.getResult());
        model.setUnfinishedCause(dto.getUnfinishedCause());
        model.setUpdateTime(new Date());

        if (CollectionUtils.isNotEmpty(dto.getAttachments())) {
            final List<ChangeImplementationAttachment> list =
                    this.changeImplementationAttachmentConverter.toListModel(dto.getAttachments());
            list.forEach(t -> t.setChangeImplementation(model));
            model.setAttachments(list);
        }

        if (CollectionUtils.isNotEmpty(dto.getChangeCiPropertyList())) {
            final List<ChangeCiPropertyDTO> listChangeCiPropertyDTO = dto.getChangeCiPropertyList();
            listChangeCiPropertyDTO.forEach(item -> {
                item.setId(null);
                item.setTaskId(model.getTaskId());
            });

            final List<ChangeCiProperty> list = this.changeCiPropertyConverter.toListModel(listChangeCiPropertyDTO);
            list.forEach(item -> {
                item.setChangeImplementation(model);
            });
            model.setChangeCiPropertyList(list);
        }

        if (CollectionUtils.isNotEmpty(dto.getChangeCiRelationList())) {
            final List<ChangeCiRelationDTO> listChangeCiRelationDTO = dto.getChangeCiRelationList();
            listChangeCiRelationDTO.forEach(item -> {
                item.setId(null);
                item.setTaskId(model.getTaskId());
            });

            final List<ChangeCiRelation> list =
                    this.changeCiRelationConverter.toListModel(listChangeCiRelationDTO);
            list.forEach(item -> {
                item.setChangeImplementation(model);
            });

            model.setChangeCiRelationList(list);
        }

        return model;
    }

    @Override
    public ChangeImplementationDTO toDTO(final ChangeImplementation model, final boolean forListView) {
        final ChangeImplementationDTO dto = new ChangeImplementationDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setActualStartTime(model.getActualStartTime());
        dto.setActualEndTime(model.getActualEndTime());
        dto.setImplementationContent(model.getImplementationContent());
        dto.setResult(model.getResult());
        dto.setUnfinishedCause(model.getUnfinishedCause());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        if (!forListView) {
            dto.setAttachments(this.changeImplementationAttachmentConverter.toListDTO(model.getAttachments()));

            if (dto.getTaskId() != null) {
                // 用 taskId 筛选关系
                final List<ChangeCiRelation> filterChangeCiRelation =
                        model.getChangeCiRelationList().stream()
                                .filter(item -> dto.getTaskId().equals(item.getTaskId())).collect(Collectors.toList());
                dto.setChangeCiRelationList(this.changeCiRelationConverter.toListDTO(filterChangeCiRelation));

                // 用 taskId 筛选属性
                final List<ChangeCiProperty> filterChangeCiProperty =
                        model.getChangeCiPropertyList().stream()
                                .filter(item -> dto.getTaskId().equals(item.getTaskId())).collect(Collectors.toList());
                dto.setChangeCiPropertyList(this.changeCiPropertyConverter.toListDTO(filterChangeCiProperty));
            } else {
                dto.setChangeCiRelationList(this.changeCiRelationConverter.toListDTO(model.getChangeCiRelationList()));
                dto.setChangeCiPropertyList(this.changeCiPropertyConverter.toListDTO(model.getChangeCiPropertyList()));
            }

        }

        return dto;
    }
}
